var userModule = angular.module('User',[]);
var temperatureModule = angular.module('Temperature',[]);
var bloodPressureModule = angular.module('BloodPressure',[]);
var ecgModule = angular.module('ECG',[]);
//心电
ecgModule.controller('ecgController',function($rootScope,$scope,$http,CookieService,rootPath){
    //请求心电数据
    var datas = "";
    $scope.queryBtn = function(){
        var token = CookieService.getCookie('token');
        if(token == null || token == ''){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:"未登录，请登录"
            }
            location.href="#/home";
            return;
        }
        var time = $('#datetimepicker').val();
        if(time == null || time == "" || time == undefined){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:"请输入查询时间"
            }
            return;
        }
        $http({
            method:'GET',
            url:rootPath + "/ecg/time",
            params:{token:token,time:time},
            async:false
        }).success(function(data,status,headers,config){
            if(data.status == ResponseStatus.SUCCESS){
                var content = data.data;
                if(content == null || content == ""){
                    $rootScope.alertInfo = {
                        show:true,
                        title:"提示",
                        content:"无当天的数据"
                    }
                    return;
                }
                $('.query-from').css("display","block");
                datas=data.data.content.split(",");
                drawecg();
                ecg(datas);
                $(".ecg-btn").attr("disabled","true");
            }else{
                $rootScope.alertInfo = {
                    show:true,
                    title:data.status,
                    content:data.message
                }
            }
        }).error(function(data,status,headers,config){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:data.message
            }
        });
    }
    //心电波形处理
    var baseineval = 128;
    var adu = 52;
    var samplingrate = 128;
    var max_times = 135;
    var points_one_times = 8;
    var gride_width = 25;
    var gx = points_one_times * (gride_width * 5 / samplingrate);
    var x_start = 25;
    var y_starts = GetYStarts(12);
    var draw_lines_index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11];
    var last_points = [[25, 100], [25, 200], [25, 300], [25, 400], [25, 500], [25, 600], [25, 700], [25, 800], [25, 900], [25, 1000], [25, 1100], [25, 1200]];
    var ecg_scope = 1;
    var current_times = 0;
    var hits = 1;
    var channels = 8;
    var count = draw_lines_index.length;
    function Queue() {
        var B = new Array();
        var A = 50;
        Queue.prototype.EnQueue = function(C) {
            if (C == null) {
                return - 1
            }
            if (B.length >= this.capacity) {
                B.remove(0)
            }
            B.push(C)
        };
        Queue.prototype.DeQueue = function() {
            if (B.length == 0) {
                return null
            } else {
                return B.shift()
            }
        };
        Queue.prototype.GetSize = function() {
            return B.length
        };
        Queue.prototype.GetHead = function() {
            if (B.length == 0) {
                return null
            } else {
                return B[0]
            }
        };
        Queue.prototype.MakeEmpty = function() {
            B.length = 0
        };
        Queue.prototype.IsEmpty = function() {
            if (B.length == 0) {
                return true
            } else {
                return false
            }
        }
    }
    var canvas = null;
    var canvasline = null;
    var ctx = null;
    var linectx = null;
    var width = 0;
    var height = 0;
    var isstop = true;
//绘制背景
    function drawecg() {
        canvas = document.getElementById("background");
        canvasline = document.getElementById("line");
        width = canvas.width;
        height = canvas.height;
        ctx = canvas.getContext("2d");
        linectx = canvasline.getContext("2d");
        drawgrid();
        addfilltext()
    }
//绘制背景表格
    function drawgrid() {
        var D = 25;
        var H = 25;
        var B = 0;
        var C = 0;
        var G = width;
        var E = height;
        ctx.lineWidth = 2;
        //ctx.strokeStyle = "blue";
        ctx.strokeStyle = "#ffbebe";
        ctx.moveTo(B, C);
        ctx.lineTo(G, C);
        ctx.stroke();
        var F = D / 5;
        while (C < E) {
            C = C + H;
            ctx.moveTo(B, C - 1);
            ctx.lineTo(G, C - 1);
            ctx.stroke()
        }
        C = 0;
        ctx.moveTo(B, C);
        ctx.lineTo(B, E);
        ctx.stroke();
        while (B < G) {
            B = B + D;
            ctx.moveTo(B - 1, C);
            ctx.lineTo(B - 1, E);
            ctx.stroke()
        }
        ctx.lineWidth = 1;
        ctx.strokeStyle = "#ffbebe";
        B = 0;
        C = 0;
        G = width;
        E = height;
        while (C < E) {
            for (var A = 0; A < 4; A++) {
                C = C + F;
                ctx.moveTo(B, C - 0.5);
                ctx.lineTo(G, C - 0.5);
                ctx.stroke()
            }
        }
        B = 0;
        C = 0;
        G = width;
        E = height;
        while (B < G) {
            for (var A = 0; A < 4; A++) {
                B = B + F;
                ctx.moveTo(B - 0.5, C);
                ctx.lineTo(B - 0.5, E);
                ctx.stroke()
            }
        }
    }
//添加导联坐标
    function addfilltext() {
        var A = ["I", "II", "III", "aVR", "aVL", "aVF", "V1", "V2", "V3", "V4", "V5", "V6"];
        var C = (height - 100) / 12;
        var D = 0;
        var B = draw_lines_index;
        for (var E = 0; E < draw_lines_index.length; E++) {
            D = D + C;
            ctx.font = "bold 14px 微软雅黑";
            ctx.fillText("" + A[draw_lines_index[E]] + "", 10, D)
        }
    }
    var oQueue = new Queue();
    function ecg(datas) {
        initparm();
        Convert16Scale(datas);
        loop()
    }
    function Convert16Scale(datas) {
        var A = datas.length;
        for (var C = 0; C < A; C++) {
            var B = datas[C];
            adddata(B, 1, 8, 128)
        }
    }
    $scope.showgrid = function(){
        var A = document.getElementById("chkshowgrid").checked;
        if (A) {
            drawgrid()
        } else {
            ctx.beginPath();
            ctx.clearRect(0, 0, width, height);
            ctx.stroke()
        }
        addfilltext()
    }
    $scope.saveclick = function() {
        oQueue.MakeEmpty();
        Convert16Scale(datas);
        var J = document.getElementsByName("radadu");
        var D;
        for (var A = 0; A < J.length; A++) {
            if (J[A].checked) {
                D = J[A].value
            }
        }
        ecg_scope = D;
        var H = new Array();
        var I = 0;
        var E = document.getElementsByName("chkecg");
        for (var B = 0; B < E.length; B++) {
            if (E[B].checked) {
                H.push(E[B].value);
                I++
            }
        }
        draw_lines_index = H;
        var F = (height - 100) / 12;
        var G = new Array();
        for (var C = 0; C < I; C++) {
            G[C] = new Array();
            G[C][0] = 25;
            G[C][1] = C * F + F
        }
        last_points = G;
        initparm();
        x_start = 25;
        y_starts = GetYStarts(I);
        linectx.beginPath();
        linectx.clearRect(0, 0, width, height);
        linectx.stroke();
        ctx.beginPath();
        ctx.clearRect(0, 0, width, height);
        ctx.stroke();
        drawgrid();
        addfilltext();
        current_times = 0;
        loop()
    }
//注入数据
    function adddata(F, C, E, J) {
        if (F == null || F.length < 4) {
            return
        } else {
            var I = new Array(F.length / 2 / C);
            if (C == 1) {
                I = F
            } else {
                for (var A = 0; A < I.length; A++) {
                    var D = "";
                    var H = F.substr(A * 2 * E * C, 2 * E * C);
                    for (var B = 0; B < C; B++) {
                        D = parseInt(H.substr(B * 2, 2), 16) + D
                    }
                    I[A] = D
                }
            }
            for (var A = 0; A < I.length / 2 / E; A++) {
                if (E == 8) {
                    var G = new Array(12);
                    var H = I.substr(A * 16, 16);
                    G[0] = parseInt(H.substr(0, 2), 16);
                    G[1] = parseInt(H.substr(2, 2), 16);
                    G[2] = G[1] - (G[0] - J);
                    G[3] = -(G[0] + G[1]) / 2 + J * 2;
                    G[4] = G[0] - (G[1] - J) / 2;
                    G[5] = G[1] - (G[0] - J) / 2;
                    G[6] = parseInt(H.substr(4, 2), 16);
                    G[7] = parseInt(H.substr(6, 2), 16);
                    G[8] = parseInt(H.substr(8, 2), 16);
                    G[9] = parseInt(H.substr(10, 2), 16);
                    G[10] = parseInt(H.substr(12, 2), 16);
                    G[11] = parseInt(H.substr(12, 2), 16);
                    oQueue.EnQueue(G)
                } else {
                    var G = new Array(E);
                    var H = I.substr(A * 16, 16);
                    for (var B = 0; B < E; B++) {
                        G[B] = parseInt(H.substr(B * 2, 2), 16)
                    }
                    oQueue.EnQueue(G)
                }
            }
        }
    }
//设置初始化线条
    function initparm() {
        if (width < 150) {
            alert("暂不支持！")
        } else {
            max_times = parseInt((width - 50) / gx)
        }
        linectx.strokeStyle = "#9d6003"
    }
    var current_time_millis = 0;
    function loop() {
        var A = new Date().getTime();
        current_time_millis = A;
        draw(y_starts, baseineval, adu, samplingrate, max_times, points_one_times, linectx, draw_lines_index);
        if (isstop) {
            setTimeout(loop, 62);
            var C = new Date().getTime();
            var B = C - current_time_millis + 1;
            if (B < 62) {
                sleep(62 - B)
            }
        }
        if (oQueue.IsEmpty()) {
            Convert16Scale(datas);
        }
    }
    function sleep(B) {
        var A = new Date().getTime();
        for (var C = 0; C < 10000000; C++) {
            if ((new Date().getTime() - A) > B) {
                break
            }
        }
    }
    function GetYStarts(C) {
        var B = new Array();
        if (C >= 3) {
            for (var A = 0; A < C; A++) {
                if (height < 480) {
                    B[A] = 100 + A * 100
                } else {
                    B[A] = 100 + A * 100
                }
            }
        } else {
            if (C == 2) {
                if (height < 480) {
                    B[0] = 130;
                    B[1] = 300
                } else {
                    B[0] = 130;
                    B[1] = 370
                }
            } else {
                if (C == 1) {
                    B[0] = 200
                } else {
                    return null
                }
            }
        }
        //console.log(B);
        return B
    }
    function draw(Q, B, P, N, G, H, A, E) {
        current_times = current_times % G;
        if (oQueue.IsEmpty()) {
            return
        }
        if (oQueue.GetSize() < H) {
            return
        }
        clearcanvans(current_times, H, N, A);
        var F = new Array();
        for (var J = 0; J < H; J++) {
            F.push(oQueue.DeQueue())
        }
        A.beginPath();
        for (var J = 0; J < E.length; J++) {
            for (var K = 0; K < F.length; K++) {
                var O = F[K];
                var C = O[E[J]] - B;
                var I = K * parseFloat((gride_width * 5 / N));
                var M;
                if (ecg_scope != 0) {
                    M = parseFloat((Math.abs(C)) * (P / (gride_width * 2)) * ecg_scope)
                } else {
                    M = parseFloat((Math.abs(C)) * (P / (gride_width * 2)) / 2)
                }
                var L = parseFloat(x_start + current_times * H * (gride_width * 5 / N));
                if (K == 0) {
                    if (current_times != 0) {
                        A.moveTo(last_points[J][0], last_points[J][1]);
                        var D = parseFloat(C >= 0 ? Q[J] - M: Q[J] + M);
                        A.lineTo(last_points[J][0], D);
                        last_points[J][0] = last_points[J][0];
                        last_points[J][1] = D
                    } else {
                        var D = parseFloat(C >= 0 ? Q[J] - M: Q[J] + M);
                        A.moveTo(x_start, D);
                        last_points[J][0] = x_start;
                        last_points[J][1] = D
                    }
                } else {
                    var D = parseFloat(C >= 0 ? Q[J] - M: Q[J] + M);
                    A.lineTo(L + I, D);
                    last_points[J][0] = L + I;
                    last_points[J][1] = D
                }
            }
        }
        A.stroke();
        current_times++
    }
    function clearcanvans(B, F, C, D) {
        var A = parseFloat(F * (gride_width * 5 / C));
        var E = x_start + B * A;
        if (B != 0) {
            D.clearRect(E, 0, 20, height)
        } else {
            D.clearRect(E - 10, 0, E + 20, height)
        }
    };

});
//血压
bloodPressureModule.controller('BloodPressureController',function($rootScope,$scope,$http,CookieService,rootPath){
    //处理日期变化
    var date = new Date();
    var year = date.getFullYear();
    for(var i = year; i > year - 3;i--){
        $('.selectYear').append('<option value="'+i+'">'+i+'</option>');
    }
    $('.selectYear').change(function(){
        $('.selectDay').empty();
        $('.selectDay').append('<option value="-1">选择天数</option>')
        var nowYear = $('.selectYear').val();
        var month = $('.selectMonth').val();
        if(month != -1){
            var maxDay = new Date(nowYear,month,0).getDate();
            for(var i = 1; i <= maxDay; i++){
                $('.selectDay').append('<option value="'+i+'">'+i+'</option>')
            }
        }
    })
    $('.selectMonth').change(function(){
        $('.selectDay').empty();
        var month = $(this).val();
        var nowYear = $(this).val();
        if(month != -1 || year != -1){
            $('.selectDay').append('<option value="-1">选择天数</option>')
            var maxDay = new Date(nowYear,month,0).getDate();
            for(var i = 1; i <= maxDay; i++){
                $('.selectDay').append('<option value="'+i+'">'+i+'</option>')
            }
        }else{
            $('.selectDay').append('<option value="-1">选择天数</option>')
        }
    })
    //处理逻辑
    $scope.queryBtn = function(){
        var token = CookieService.getCookie('token');
        if(token == null || token == ''){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:"未登录，请登录"
            }
            return;
        }
        var year = $('.selectYear').val();
        var month = $('.selectMonth').val();
        var day = $('.selectDay').val();
        if(year == -1){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:"请选择年份"
            }
            return;
        }
        var urlParams = '';
        if(month == -1){
            urlParams = "/" + year + "/3";
        }else if( day == -1){
            urlParams = "/" +year+"-"+month+ "/1";
        }else{
            urlParams = "/"+year +"-"+month+"-"+day+"/2";
        }
        $http({
            method:'GET',
            url:rootPath + "/bloodPressure" + urlParams,
            params:{token:token}
        }).success(function(data,status,headers,config){
            if(data.status == ResponseStatus.SUCCESS){
                var length = data.data.length;
                if(length == 0){
                    $rootScope.alertInfo = {
                        show:true,
                        title:'提示',
                        content:'无数据'
                    }
                    return;
                }
                var showHtml = '<tr> <th>#</th> <th>最高血压/mmHg</th><th>最低血压/mmHg</th> <th>脉搏/分</th> <th>时间</th></tr>';
                var showData = new Array();
                var data0 = new Array();
                var data1 = new Array();
                var data2 = new Array();
                for(var i = 0; i < data.data.length; i++){
                    var d = data.data[i];
                    data0.push({
                        x: d.gmtCreate,
                        y: d.highPressure,
                    });
                    data1.push({
                        x: d.gmtCreate,
                        y: d.lowPressure,
                    });
                    data2.push({
                        x: d.gmtCreate,
                        y: d.pulse,
                    })
                    var highClass = bloodPressureUtils.hightPressureLevel(d.highPressure).showClass;
                    var highNotes = d.highPressure + '/'+ bloodPressureUtils.hightPressureLevel(d.highPressure).notes;
                    var lowClass = bloodPressureUtils.lowPressureLevel(d.lowPressure).showClass;
                    var lowNotes = d.lowPressure + '/'+ bloodPressureUtils.lowPressureLevel(d.lowPressure).notes;
                    var pulseClass = bloodPressureUtils.pulseLevel(d.pulse).showClass;
                    var pulseNotes = d.pulse + '/'+ bloodPressureUtils.pulseLevel(d.pulse).notes;
                    showHtml += '<tr><td>'+(i+1)+'</td>' +
                        '<td class="'+highClass+'">'+highNotes+'</td>' +
                        '<td class="'+lowClass+'">'+lowNotes+'</td>' +
                        '<td class="'+pulseClass+'">'+pulseNotes+'</td>' +
                        '<td>'+Highcharts.dateFormat('%Y-%m-%d %H:%M', d.gmtCreate)+'</td></tr>';
                }
                showData.push(data0);
                showData.push(data1);
                showData.push(data2);
                var date = year+(month==-1?'':'-'+month)+(day==-1?'':'-'+day);
                lineChart(showData,date);
                $('.showTable').before('<h3 style="text-align:center;">详细信息</h3>');
                $('.showTable').html(showHtml);
            }else{
                $rootScope.alertInfo = {
                    show:true,
                    title:data.status,
                    content:data.message
                }
            }
        }).error(function(data,status,headers,config){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:data.message
            }
        });

    }
    //折线图逻辑
    var lineChart = function(data,date){
        console.log(data);
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });
        var chart;
        $('#lineChart').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function() {
                        // set up the updating of the chart each second
                        var len = data[0].length;
                        var i = 5;
                        var series_highP = this.series[0];
                        var series_lowP = this.series[1];
                        var series_pulse = this.series[2];
                        setInterval(function() {
                            if(i == length){
                                clearInterval();
                            }
                            var x = data[0][i].x, // current time
                                y = data[0][i].y;
                            var x1 = data[1][i].x, // current time
                                y1 = data[1][i].y;
                            var x2 = data[2][i].x, // current time
                                y2 = data[2][i].y;
                            series_highP.addPoint([x, y], true, true);
                            series_lowP.addPoint([x1, y1], true, true);
                            series_pulse.addPoint([x2, y2], true, true);
                            i++;
                        }, 3000);
                    }
                }
            },
            title: {
                text: date+'--血压统计图'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 100,
            },
            yAxis: {
                title: {
                    text: '血压/mmHg 脉搏/分'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }],
                max:160,
                min:40,
                tickPixelInterval:20,
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.series.name +'</b><br/>'+
                        Highcharts.dateFormat('%Y-%m-%d %H:%M', this.x) +'<br/>'+
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                layout: 'vertical',
                backgroundColor: '#FFFFFF',
                floating: true,
                align: 'right',
                verticalAlign: 'top',
                y:20,
                margin:20,
                labelFormatter: function() {
                    return '<span style="color:'+this.color+'">'+this.name+'</span>'
                },
            },
            plotOptions: {
                series: {
                    events: {
                        legendItemClick: function() {
                            // return false 即可禁止图例点击响应
                            return false;
                        }
                    }
                }
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: '最高血压',
                lineWidth:1,
                dashStyle: 'solid',
                data: (function() {
                    var len = data[0].length;
                    if(len > 5){
                        return new Array(data[0][0],data[0][1],data[0][2],data[0][3],data[0][4]);
                    }else{
                        return data[0];
                    }
                })()
            },{
                name: '最低血压',
                lineWidth:1,
                dashStyle: 'longdash',
                data: (function() {
                    var len = data[0].length;
                    if(len > 5){
                        return new Array(data[1][0],data[1][1],data[1][2],data[1][3],data[1][4]);
                    }else{
                        return data[1];
                    }
                })()
            },{
                name: '脉搏',
                lineWidth:1,
                dashStyle: 'shortdot',
                data: (function() {
                    var len = data[0].length;
                    if(len > 5){
                        return new Array(data[2][0],data[2][1],data[2][2],data[2][3],data[2][4]);
                    }else{
                        return data[0];
                    }
                })()
            }
            ]
        });
    }

})
//温度
temperatureModule.controller('TemperatureController',function($rootScope,$scope,$http,CookieService,rootPath){
    $scope.queryBtn = function(){
        var token = CookieService.getCookie('token');
        if(token == null || token == ''){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:"未登录，请登录"
            }
            return;
        }
        var month = $('.selectMonth').val();
        var day = $('.selectDay').val();
        if(month == -1){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:"请选择月份"
            }
            return;
        }
        var urlParams = '';
        if(day == -1){
            urlParams = "/" + month + "/1";
        }else{
            urlParams = "/" +month+"-"+day+ "/2";
        }
        $http({
            method:'GET',
            url:rootPath + "/temperature" + urlParams,
            params:{token:token}
        }).success(function(data,status,headers,config){
            if(data.status == ResponseStatus.SUCCESS){
                var length = data.data.length;
                if(length == 0){
                    $rootScope.alertInfo = {
                        show:true,
                        title:'提示',
                        content:'无当天的数据'
                    }
                    return;
                }
                var showData = new Array();
                var showTable = '<tr><th>#</th><th>温度</th><th>建议</th><th>时间</th></tr>'
                for(var i = 0; i < length; i++){
                    showData.push({
                       x:data.data[i].gmtCreate,
                        y:data.data[i].size
                    });
                    showTable += '<tr class="'+temperatureUtils.level(data.data[i].size).showClass+'">'
                    showTable += '<td>'+(i+1)+'</td>'
                    showTable += '<td>'+data.data[i].size+'℃'+'</td>'
                    showTable += '<td>'+temperatureUtils.level(data.data[i].size).notes+'</td>'
                    showTable += '<td>'+Highcharts.dateFormat('%Y-%m-%d %H:%M', data.data[i].gmtCreate)+'</td>'
                    showTable +='</tr>'
                }
                var date = new Date();
                var year = date.getFullYear();
                var showDate = year + '年' + month + '月' +(day==-1?'':day+'日');
                lineChart(showData,showDate);
                $('.showTable').before('<h3 style="text-align: center;">详细信息</h3>');
                $('.showTable').html(showTable);
            }else{
                $rootScope.alertInfo = {
                    show:true,
                    title:data.status,
                    content:data.message
                }
            }
        }).error(function(data,status,headers,config){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:data.message
            }
        });

    }
    $('.selectMonth').change(function(){
        $('.selectDay').empty();
        var month = $(this).val();
        if(month != -1){
            $('.selectDay').append('<option value="-1">选择天数</option>')
            var date = new Date();
            var year = date.getFullYear();
            var maxDay = new Date(year,month,0).getDate();
            for(var i = 1; i <= maxDay; i++){
                $('.selectDay').append('<option value="'+i+'">'+i+'</option>')
            }
        }else{
            $('.selectDay').append('<option value="-1">选择天数</option>')
        }
    })
    var lineChart = function(data,date){
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });
        var chart;
        $('#lineChart').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function() {
                        var series = this.series[0];
                        var length = data.length;
                        i = 5;
                        setInterval(function() {
                            if(i == length){
                                clearInterval();
                            }
                            var x = data[i].x, // current time
                                y = data[i].y;
                            i++;
                            series.addPoint([x, y], true, true);
                        }, 3000);
                    }
                }
            },
            title: {
                text: date+'--统计数据'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 100
            },
            yAxis: {
                max:42,
                min:34,
                tickPixelInterval:50,
                title: {
                    text: '温度统计（℃）'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ '温度:'+'</b>'+this.y+'<br><b>'+'日期:'+
                        Highcharts.dateFormat('%Y-%m-%d %H:%M', this.x) +'</b>';
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'temperature data',
                data: (function() {
                    var len = data.length;
                    if(len > 5){
                        return new Array(data[0],data[1],data[2],data[3],data[4]);
                    }else{
                        return data;
                    }
                })()
            }]
        });
    }
})
//用户模块
userModule.controller('UpdateUserController',function($rootScope,$scope,$http,CookieService,rootPath){
    $scope.formStep = true;
    $scope.toStep1 = function(){
        $scope.formStep = true;
    }
    $scope.toStep2 = function(){
        $scope.formStep = false;
    }
    $scope.nickname = CookieService.getCookie('user.nickname');
    $scope.age = CookieService.getCookie('user.age');
    $scope.gender = CookieService.getCookie('user.gender');
    $scope.$watch('nickname',function(){
        $scope.isFail = false;
    });
    $scope.$watch('gender',function(){
        $scope.isFail = false;
    });
    $scope.$watch('age',function(newVal){
        $scope.isFail = true;
        if(newVal != undefined && newVal != null && newVal != ''){
            if(isNaN(newVal)){
                $scope.ageFail = true;
            }else if(newVal <= 0 || newVal > 150){
                $scope.ageFail = true;
            }else{
                $scope.ageFail = false;
            }
        }
    });
    $scope.infoSubmit = function(){
        var nickname = $scope.nickname;
        var age = $scope.age;
        var gender = $scope.gender;
        if(nickname == undefined || nickname == null || nickname == ''){
            if(age == undefined || age == null || age == ''){
                if(gender == undefined || gender == null || gender == ''){
                    $scope.isFail = true;
                    $scope.failInfo = '请输入要修改的数据';
                    return;
                }
            }else{
                if(isNaN(age)){
                    $scope.ageFail = true;
                    return;
                }else if(newVal <= 0 || newVal > 150){
                    $scope.ageFail = true;
                    return;
                }else{
                    $scope.ageFail = false;
                }
            }
        }
        if(age != undefined && age != null && age != ''){
            if(isNaN(age)){
                $scope.ageFail = true;
                return;
            }else if(age <= 0 || age > 150){
                $scope.ageFail = true;
                return;
            }else{
                $scope.ageFail = false;
            }
        }
        if(nickname == CookieService.getCookie('user.nickname')){
            if(age == CookieService.getCookie('user.age')){
                if(gender == CookieService.getCookie('user.gender')){
                    $scope.isFail = true;
                    $scope.failInfo = '未修改数据！';
                    return;
                }
            }
        }
        var token = CookieService.getCookie("token");
        var params = {nickname:nickname, age:age, gender:gender,token:token};
        $http({
            method:'POST',
            url:rootPath+"/user",
            params:params,
        }).success(function(data,status,headers,config){
            console.log(JSON.stringify(data));
            if(data.status == ResponseStatus.SUCCESS){
                CookieService.delCookie('user.nickname');
                CookieService.delCookie('token');
                $rootScope.alertInfo = {
                    show:true,
                    title:"提示",
                    content:"修改成功，请重新登录！"
                }
            } else{
                $rootScope.alertInfo = {
                    show:true,
                    title:"提示",
                    content:data.message
                }
            }
        }).error(function(data,status,headers,config){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:"请求不到服务器"
            }
        });

    }
    $scope.$watch('password',function(){
        $scope.pwFail = false;
    });
    $scope.psSubmit = function(){
        var password = $scope.password;
        if(password == undefined || password == null || password == ''){
            $scope.pwFail = true;
            return;
        }
        $http({
            method:'PUT',
            url:rootPath+"/user",
            params:{password:password}
        }).success(function(data,status,headers,config){
            console.log(JSON.stringify(data));
            if(data.status == ResponseStatus.SUCCESS){
                CookieService.delCookie('user.nickname');
                CookieService.delCookie('token');
                $rootScope.alertInfo = {
                    show:true,
                    title:"提示",
                    content:"修改密码成功，请重新登录！"
                }
            } else{
                $rootScope.alertInfo = {
                    show:true,
                    title:"提示",
                    content:data.message
                }
            }
        }).error(function(data,status,headers,config){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:data.message
            }
        });
    }

})
userModule.controller('SignUpController',function($rootScope,$scope,$http,CookieService,rootPath){
    $scope.signUpFormStep = true;

    $scope.$watch('addNickname',function(){
        $scope.nicknameFailInfo = false;
    });
    $scope.$watch('addAge',function(newVal){
        if(newVal != undefined && newVal != null && newVal != ''){
            if(isNaN(newVal)){
                $scope.ageFailInfo = true;
            }else if(newVal <= 0 || newVal > 150){
                $scope.ageFailInfo = true;
            }else{
                $scope.ageFailInfo = false;
            }
        }
    });
    $scope.$watch('addGender',function(){
        $scope.genderFailInfo = false;
    });
    $scope.$watch('addAccount',function(){
        $scope.accountFailInfo = false;
    });
    $scope.$watch('addPassword',function(){
        $scope.passwordFailInfo = false;
    });
    $scope.setAccount = function(){
        var nickname = $scope.addNickname;
        var age = $scope.addAge;
        var gender = $scope.addGender;
        if(nickname == undefined || nickname == null || nickname == ''){
            $scope.nicknameFailInfo = true;
            return;
        }
        if(age == undefined || age == null || age == ''){
            $scope.ageFailInfo = true;
            return;
        }else{
            if(isNaN(age)){
                $scope.ageFailInfo = true;
                return;
            }else if(age <= 0 || age > 150){
                $scope.ageFailInfo = true;
                return;
            }
        }
        if(gender == undefined || gender == null || gender == ''){
            $scope.genderFailInfo = true;
            return;
        }
        $scope.signUpFormStep = false;
    };
    $scope.submit = function(){
        var account = $scope.addAccount;
        var password = $scope.addPassword;
        if(account == undefined || account == null || account == ''){
            $scope.accountFailInfo = true;
        }
        if(password == undefined || password == null || password == ''){
            $scope.passwordFailInfo = true;
        }
        var nickname = $scope.addNickname;
        var age = $scope.addAge;
        var gender = $scope.addGender;
        var params = {
            nickname:nickname,
            age:age,
            gender:gender,
            account:account,
            password:password
        }
        $http({
            method:'PUT',
            url:rootPath+"/user",
            params:params
        }).success(function(data,status,headers,config){
            console.log(JSON.stringify(data));
            if(data.status == ResponseStatus.SUCCESS){
                CookieService.setCookie('token',data.data.token,1/24);
                CookieService.setCookie('user.nickname',nickname,1/24);
                CookieService.setCookie('user.age',age,1/24);
                CookieService.setCookie('user.gender',gender,1/24);
                $rootScope.alertInfo = {
                    show:true,
                    title:"提示",
                    content:"注册成功"
                }
            }else if(data.status == ResponseStatus.OTHER){
                $rootScope.alertInfo = {
                    show:true,
                    title:"success",
                    content:'注册成功'
                }
            }else{
                $rootScope.alertInfo = {
                    show:true,
                    title:"提示",
                    content:data.message
                }
            }
        }).error(function(data,status,headers,config){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:data.message
            }
        });
    };
    $scope.toStep1 = function(){
        $scope.signUpFormStep = true;
    }
})
userModule.controller('SignInController',function($rootScope,$scope,$http,CookieService,rootPath){
    $scope.$watch('account',function(){
        $scope.accountFailInfo = false;
    })
    $scope.$watch('password',function(){
        $scope.passwordFailInfo = false;
    })
    $scope.signIn = function(){
        var account = $scope.account;
        var password = $scope.password;
        if(account == null || account == ""){
            $scope.accountFailInfo = true;
            return;
        }
        if(password == null || password == ""){
            $scope.passwordFailInfo = true;
            return;
        }
        var params = {account:account,password:password};
        $http({
            method:'GET',
            url:rootPath+"/user",
            params:params
        }).success(function(data,status,headers,config){
            console.log(JSON.stringify(data));
            if(data.status == ResponseStatus.SUCCESS){
                CookieService.setCookie('token',data.data.token,1/24);
                CookieService.setCookie('user.nickname',data.data.nickname,1/24);
                CookieService.setCookie('user.age',data.data.age,1/24);
                CookieService.setCookie('user.gender',data.data.gender,1/24);
                $rootScope.alertInfo = {
                    show:true,
                    title:"提示",
                    content:"登录成功"
                }
            }else{
                $rootScope.alertInfo = {
                    show:true,
                    title:"error",
                    content:data.message
                }
            }
        }).error(function(data,status,headers,config){
            $rootScope.alertInfo = {
                show:true,
                title:"提示",
                content:"服务器连接失败！"
            }
        });
    }
});
userModule.controller('HeaderController',function($rootScope,$scope,CookieService){
    //检查是否登录
    var nickname = CookieService.getCookie("user.nickname");
    if(nickname != undefined && nickname != null && nickname != ''){
        $scope.user = {
            nickname:nickname
        }
        $('.isNotSignIn').addClass('hide');
        $('.isSignIn').removeClass('hide');
    }else{
        $('.isSignIn').addClass('hide');
        $('.isNotSignIn').removeClass('hide');
    }
    //注销
    $scope.logout = function(){
        CookieService.delCookie("token");
        CookieService.delCookie("user.nickname");
        $rootScope.alertInfo = {
            show:true,
            title:"提示",
            content:"注销成功"
        }
    }
})